//
//  BiTabBar.m
//  GottaWash
//
//  Created by bindu on 14-8-7.
//  Copyright (c) 2014年 QiHe. All rights reserved.
//

#import "BiTabBar.h"

static int kInterTabMargin = 0;

@implementation BiTabBar

#pragma mark - Initialization

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.userInteractionEnabled = YES;
        self.contentMode = UIViewContentModeRedraw;
        self.opaque = YES;
        self.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
                                 UIViewAutoresizingFlexibleHeight |
                                 UIViewAutoresizingFlexibleTopMargin);
    }
    return self;
}

#pragma mark - Setters and Getters

- (void)setTabs:(NSArray *)array
{
    if (_tabs != array)
    {
        if (nil != _tabs)
        {
            [_tabs release];
            _tabs = nil;
        }
        
        for (BiTab *tab in _tabs)
        {
            [tab removeFromSuperview];
        }
        
        _tabs = [array retain];
        
        for (BiTab *tab in _tabs) {
            tab.userInteractionEnabled = YES;
            [tab addTarget:self action:@selector(tabSelected:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    [self setNeedsLayout];
}

#pragma mark - Delegate notification

- (void)tabSelected:(BiTab *)sender
{
    [_delegate tabBar:self didSelectTabAtIndex:[_tabs indexOfObject:sender]];
}


#pragma mark - Drawing & Layout

- (void)drawRect:(CGRect)rect
{
    // Drawing the tab bar background
	CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    // Drawing the top line
    CGContextSaveGState(ctx);
    {
        _edgeColor = UIColorFromRGB(0xb3b3b3);
        CGContextSetFillColorWithColor(ctx, [_edgeColor CGColor]);
        CGContextFillRect(ctx, CGRectMake(0, 0, rect.size.width, 1));
    }
    CGContextRestoreGState(ctx);
    
    // Drawing the top dark emboss
    CGContextSaveGState(ctx);
    {
        UIColor * bgColor = UIColorFromRGB(0xffffff);
        CGContextSetFillColorWithColor(ctx, [bgColor CGColor]);
        CGContextFillRect(ctx, CGRectMake(0, 1, rect.size.width, rect.size.height));
    }
    CGContextRestoreGState(ctx);
    
    for (BiTab *tab in _tabs)
    {
        CGContextFillRect(ctx, CGRectMake(tab.frame.origin.x - kInterTabMargin, 0, kInterTabMargin, rect.size.height));
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat screenWidth = self.bounds.size.width;
    
    CGFloat tabNumber = _tabs.count;
    
    // Calculating the tabs width.
    CGFloat tabWidth = floorf(((screenWidth + 1) / tabNumber) - 1);
    
    // Because of the screen size, it is impossible to have tabs with the same
    // width. Therefore we have to increase each tab width by one until we spend
    // of the spaceLeft counter.
    CGFloat spaceLeft = screenWidth - (tabWidth * tabNumber) - (tabNumber - 1);
    
    CGRect rect = self.bounds;
    rect.size.width = tabWidth;
    
    CGFloat dTabWith;
    
    for (BiTab *tab in _tabs) {
        
        // Here is the code that increment the width until we use all the space left
        dTabWith = tabWidth;
        
        if (spaceLeft != 0) {
            dTabWith = tabWidth + 1;
            spaceLeft--;
        }
        
        if ([_tabs indexOfObject:tab] == 0) {
            tab.frame = CGRectMake(rect.origin.x, rect.origin.y, dTabWith, rect.size.height);
        } else {
            tab.frame = CGRectMake(rect.origin.x + kInterTabMargin, rect.origin.y, dTabWith, rect.size.height);
        }
        
        [self addSubview:tab];
        rect.origin.x = tab.frame.origin.x + tab.frame.size.width;
    }
    
}

@end
